package com.woke.addressactivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.CheckpaypswActivity;
import com.example.woke.PswchangeActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_usertypoe;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import org.greenrobot.eventbus.EventBus;

public class Userinfo_typeActivity extends Activity implements OnClickListener {

	private MyApp application;
	private String user_type;
	private TextView mTok1,mTok2,mTok3;
	private AlertDialog alertDialog;
	private View line1;
	private View line2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo_type);
		
		application = (MyApp) getApplication();
		if(application.getDatas_load ()!=null){
		user_type = application.getDatas_load().getUser_type();
		}
		intview();
		
	}

	private void intview() {
		findViewById(R.id.user_type_back).setOnClickListener(this);
		mTok1 = (TextView) findViewById(R.id.user_type_text1);
		mTok2 = (TextView) findViewById(R.id.user_type_text2);
		mTok3 = (TextView) findViewById(R.id.user_type_text3);
		line1 = findViewById(R.id.user_type_liner2);
		line2 = findViewById(R.id.user_type_liner3);
		mTok1.setOnClickListener(this);
		mTok2.setOnClickListener(this);
		mTok3.setOnClickListener(this);
		makechange();
		
	}
	
	private void makechange() {
		if(user_type.equals("2")){
			line2.setVisibility(View.GONE);
		}else if(user_type.equals("3")){
			line2.setVisibility(View.VISIBLE);
			line1.setVisibility(View.GONE);
			mTok1.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
		}else if(user_type.equals("4")){
			mTok1.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
			mTok2.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
			mTok3.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
		}
	}

	private void aralogid1(String info,String kll,final int ppp){
		   alertDialog = new AlertDialog.Builder(this). 
	                setTitle(info). 
	                setMessage(kll). 
//	                setIcon(R.drawable.ic_launcher). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    Intent intent = new Intent();
	                    		intent.setClass(Userinfo_typeActivity.this, PswchangeActivity.class);
	                    		intent.putExtra("name", 0);
	                    		startActivity(intent);
	                    		alertDialog.dismiss();
	                    } 
	                }). 
	                setNegativeButton("取消",new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	alertDialog.dismiss();
	                        // TODO Auto-generated method stub  
	                    } 
	                }).create(); 
	        alertDialog.show(); 
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 700 && resultCode == 600){
			paymoney(1);
		}else if(requestCode == 701 && resultCode == 600){
			paymoney(2);
		}else if(requestCode == 702 && resultCode == 600){
			paymoney(2);
		}
	}
	
	private void paymoney(final int i) {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "memberUp");
		params.put("userid", application.getDatas_load().getId());
		params.put("type", i);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("shengji", "成功"+response);
				try {
					String flag = response.getString("flag");
					final String info = response.getString("info");
					if(flag.equals("success")){
//						JSONObject object = response.getJSONObject("data");
//						JSONObject jsonObject = object.getJSONObject("data");
						Toast.makeText(Userinfo_typeActivity.this, info, Toast.LENGTH_SHORT).show();
						Datas_usertypoe dsts = new Datas_usertypoe();
						if(i == 1){
							user_type = "3";
							application.getDatas_load().setUser_type("3");
							dsts.ssss = "银牌";
							EventBus.getDefault().post(dsts);
						}if(i == 2){
							user_type = "4";
							application.getDatas_load().setUser_type("4");
							dsts.ssss = "金牌";
							EventBus.getDefault().post(dsts);
						}if(i == 3){
							user_type = "4";
							application.getDatas_load().setUser_type("4");
							dsts.ssss = "金牌";
							EventBus.getDefault().post(dsts);
						}
						makechange();
//						finish();
					}else {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(Userinfo_typeActivity.this,info,Toast.LENGTH_SHORT).show();
							}
						});
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	@Override
	public void onClick(View view) {
		Intent intent = new Intent();
		switch (view.getId()) {
		case R.id.user_type_back:
			finish();
			break;
			
		case R.id.user_type_text1:
			if(application.getDatas_load ()==null)return;
			if(null == application.getDatas_load().getPay_pwd() || application.getDatas_load().getPay_pwd().equals("")){
				Toast.makeText(this, "请设置支付密码", Toast.LENGTH_SHORT).show();
				aralogid1("还未设置支付密码","前往设置？",1);
			}else if(user_type.equals("3")){
				Toast.makeText(this, "您已经是银牌会员了", Toast.LENGTH_SHORT).show();
			}else if(user_type.equals("4")){
				Toast.makeText(this, "您已经是金牌会员了", Toast.LENGTH_SHORT).show();
			}else{
//				Intent intent = new Intent(Userinfo_typeActivity.this,CheckpaypswActivity.class);
				intent.setClass(Userinfo_typeActivity.this,CheckpaypswActivity.class);
				intent.putExtra("paymoney", "100");
				startActivityForResult(intent, 700);
			}
			break;
			
		case R.id.user_type_text2:
			if(null == application.getDatas_load().getPay_pwd() || application.getDatas_load().getPay_pwd().equals("")){
				Toast.makeText(this, "请设置支付密码", Toast.LENGTH_SHORT).show();
				aralogid1("还未设置支付密码","前往设置？",1);
			}else if(user_type.equals("4")){
				Toast.makeText(this, "您已经是金牌会员了", Toast.LENGTH_SHORT).show();
			}else{
				intent.setClass(Userinfo_typeActivity.this,CheckpaypswActivity.class);
				intent.putExtra("paymoney", "200");
				startActivityForResult(intent, 701);
			}
			break;
			
		case R.id.user_type_text3:
			if(null == application.getDatas_load().getPay_pwd() || application.getDatas_load().getPay_pwd().equals("")){
				Toast.makeText(this, "请设置支付密码", Toast.LENGTH_SHORT).show();
				aralogid1("还未设置支付密码","前往设置？",1);
			}else if(user_type.equals("4")){
				Toast.makeText(this, "您已经是金牌会员了", Toast.LENGTH_SHORT).show();
			}else{
				intent.setClass(Userinfo_typeActivity.this,CheckpaypswActivity.class);
				intent.putExtra("paymoney", "100");
				startActivityForResult(intent, 702);
			}
			break;

		default:
			break;
		}
	}
}
